package cn.micai.base.lambda;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 描述：Java 8 – Convert Map to List
 * <p>
 *
 * @author: 赵新国
 * @date: 2018/6/22 11:13
 */
public class MapToListExample {

    public static void main(String [] args) {

        // 1. Map To List
        Map<Integer, String> map = new HashMap<>();
        map.put(10, "apple");
        map.put(20, "orange");
        map.put(30, "banana");
        map.put(40, "watermelon");
        map.put(50, "dragonfruit");

        /*System.out.println("\n1. Export Map Key to List...");

        List<Integer> result = new ArrayList<>(map.keySet());
        result.forEach(System.out::println);

        System.out.println("\n2. Export Map Value to List...");

        List<String> result2 = new ArrayList<>(map.values());
        result2.forEach(System.out::println);*/


        // Java 8 – Map To List

        /*System.out.println("\n1. Export Map Key to List...");
        List<Integer> result = map.keySet().stream()
                .collect(Collectors.toList());
        result.forEach(System.out::println);

        System.out.println("\n2. Export Map Value to List...");
        List<String> result2 = map.values().stream()
                .collect(Collectors.toList());
        result2.forEach(System.out::println);

        System.out.println("\n3. Export Map Value to List..., say no to banana");
        List<String> result3 = map.values().stream()
                .filter(x -> !"banana".equalsIgnoreCase(x))
                .collect(Collectors.toList());
        result3.forEach(System.out::println);*/


        // 3. Java 8 – Convert Map into 2 List

        // split a map into 2 List
        List<Integer> resultSortedKey = new ArrayList<>();
        List<String> resultValues = map.entrySet().stream()
                // sort a Map by key and sorted in resultSortedKey
                .sorted(Map.Entry.<Integer, String>comparingByKey().reversed())
                .peek(e -> resultSortedKey.add(e.getKey()))
                .map(x -> x.getValue())
                // filter banana and return it to resultValues
                .filter(x -> !"banana".equalsIgnoreCase(x))
                .collect(Collectors.toList());

        resultSortedKey.forEach(System.out::println);
        resultValues.forEach(System.out::println);

    }
}
